/*
 * Copyright (c) 2017. 深圳一七科技. All rights reserved.
 */

package com.yiqi.liebang.feature.login.presenter;

import android.net.Uri;
import android.text.TextUtils;

import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.MD5Util;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.utils.ValidatorUtil;
import com.suozhang.framework.utils.logger.Logger;
import com.yiqi.liebang.common.rongim.SealAppContext;
import com.yiqi.liebang.entity.bo.RegisterBo;
import com.yiqi.liebang.feature.login.contract.LoginContract;
import com.yiqi.liebang.feature.login.dependencies.login.DaggerLoginPresenterComponent;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.rong.imkit.RongIM;
import io.rong.imlib.model.UserInfo;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2018/7/11 9:31
 */
public class LoginPresenter implements LoginContract.Presenter {

    private LoginContract.View mView;

    @Inject

    LoginContract.Model mModel;


    public LoginPresenter(LoginContract.View mView) {
        this.mView = mView;
        DaggerLoginPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void userRegister(String mobile, String code, String pwd, String nick, String registrationId) {

        if (!ValidatorUtil.isMobile(mobile)) {
            T.showShort("手机号格式不正确！");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            T.showShort("验证码不能为空！");
            return;
        }

        if (TextUtils.isEmpty(pwd)) {
            T.showShort("密码不能为空！");
            return;
        }
        if (pwd.length() < 6) {
            T.showShort("密码不能小于6位");
            return;
        }
        if (TextUtils.isEmpty(nick)) {
            T.showShort("昵称不能为空！");
            return;
        }
        pwd = MD5Util.md5(pwd);
        RegisterBo registerBo = new RegisterBo(mobile, code, pwd, nick, registrationId);
        mModel.userRegister(registerBo).compose(mView.<UserInfoBo>bindToLife()).subscribe(new Observer<UserInfoBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(UserInfoBo s) {
                mView.dismissLoading();
                loginSuccess(s);
                mView.showReisterSuccess();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();

            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });

    }

    @Override
    public void userLogin(String mobile, String pwd, String registrationId) {
        if (!ValidatorUtil.isMobile(mobile)) {
            T.showShort("手机号格式不正确！");
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            T.showShort("密码不能为空！");
            return;
        }
        if (pwd.length() < 6) {
            T.showShort("密码不能小于6位");
            return;
        }
        pwd = MD5Util.md5(pwd);
        RegisterBo registerBo = new RegisterBo(mobile, pwd);
        registerBo.setRegistrationId(registrationId);
        mModel.userLogin(registerBo).compose(mView.<UserInfoBo>bindToLife()).subscribe(new Observer<UserInfoBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(UserInfoBo s) {
                mView.dismissLoading();
                loginSuccess(s);
                mView.showLoginSuccess();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void thirdLogin(String userOpenid, String userName, String registrationId, String head,int type) {
        RegisterBo registerBo = new RegisterBo();
        registerBo.setUserOpenid(userOpenid);
        registerBo.setUserName(userName);
        registerBo.setRegistrationId(registrationId);
        registerBo.setUserHead(head);
        registerBo.setType(type);
        mModel.thirdLogin(registerBo).compose(mView.<UserInfoBo>bindToLife()).subscribe(new Observer<UserInfoBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(UserInfoBo s) {
                mView.dismissLoading();
                mView.showThirdLoginSuccess(s);
                loginSuccess(s);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void sendCode(String mobile) {

        if (!ValidatorUtil.isMobile(mobile)) {
            T.showShort("手机号格式不正确！");
            return;
        }
        RegisterBo registerBo = new RegisterBo(mobile);
        mModel.sendCode(registerBo).compose(mView.<RegisterBo>bindToLife()).subscribe(new Observer<RegisterBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(RegisterBo s) {
                mView.dismissLoading();
                mView.showCodeSuccess(s);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void userResetPwd(String mobile, String code, String pwd) {
        if (!ValidatorUtil.isMobile(mobile)) {
            T.showShort("手机号格式不正确！");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            T.showShort("验证码不能为空！");
            return;
        }

        if (TextUtils.isEmpty(pwd)) {
            T.showShort("密码不能为空！");
            return;
        }
        if (pwd.length() < 6) {
            T.showShort("密码不能小于6位");
            return;
        }

        pwd = MD5Util.md5(pwd);
        RegisterBo registerBo = new RegisterBo(mobile, code, pwd);
        mModel.userResetPwd(registerBo).compose(mView.<UserInfoBo>bindToLife()).subscribe(new Observer<UserInfoBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(UserInfoBo s) {
                mView.dismissLoading();
//                loginSuccess(s);
                mView.showResetSuccess(s);
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();

            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });

    }

    private void loginSuccess(UserInfoBo value) {

        Logger.d("登录成功 ==== " + value.toString());
        AM.user().saveLoginResultCache(value == null ? null : value);
        if (!TextUtils.isEmpty(value.getRongCloudToken())) {
            RongIM.getInstance().setCurrentUserInfo(new UserInfo(value.getUserUid(), value.getUserName(),
                    TextUtils.isEmpty(value.getUserHead()) ? Uri.parse("") : Uri.parse(value.getUserHead())));
            RongIM.getInstance().setMessageAttachedUserInfo(true);
            RongIM.connect(value.getRongCloudToken(), SealAppContext.getInstance().getConnectCallback());
        } else {
            T.showShort("融云连接失败!");
        }
        mView.showLoginSuccess();
    }


}
